<?php
namespace app\student\controller;
use app\common\Base;
use app\common\StudentModel;
use app\common\Excel;
use think\Db;

class StudentManage extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function update() {
		$ID = request()->param("id");
		if($ID > 0) {
			$where["ID"] = $ID;
			$result = Db::table("学生信息")->where($where)->find();
			$result = findResult($result);
			$gradeKey = "";
			if(array_key_exists("年级号", $result) && $result["年级号"] > 0) {
				$gradeKey = $result["年级号"]."_".$result["学籍类型"];
			}
			$result["gradeKey"] = $gradeKey;
			$this->assign("detail", $result);
		}else {
			$this->assign("detail", []);
		}
		return $this->fetch();
	}

	public function pageQuery() {
		$grade = input("grade");
		$clazz = input("clazz");
		$name = input("studentName");
		$page = input("page");
		$rows = input("rows");
		$where["转出学期ID"] = array("eq", 0);
		$where["年级号"] = array("gt", currTerm()["finishedKey"]);
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($clazz) $where["班号"] = array("eq", $clazz);
		if($name) $where["姓名"] = array("like", $name."%");
		$fields = [];
		$Model = new StudentModel();
		return $Model->getStudentList($page, $rows, $fields, $where, null);
	}

	public function exportExcel() {
		$grade = input("grade");
		$clazz = input("clazz");
		$name = input("studentName");
		$where["转出学期ID"] = array("eq", 0);
		$where["年级号"] = array("gt", currTerm()["finishedKey"]);
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($clazz) $where["班号"] = array("eq", $clazz);
		if($name) $where["姓名"] = array("like", $name."%");
		$fields = [];
		$Model = new StudentModel();
		$result = $Model->getStudentList(1, PHP_INT_MAX, $fields, $where, null);
		//excel表头内容
		$header = array('年级名称'=>'gradeName','班号'=>'班号','学号'=>'学号','姓名'=>'姓名','性别'=>'性别','身份证类型'=>'身份证类型','身份证号'=>'身份证号','生日'=>'生日',	'民族'=>'民族','国籍'=>'国籍','户口性质'=>'户口性质','户籍类别'=>'户籍类别','户籍省市区'=>'户籍省市区','户籍所在详细地址'=>'户籍所在详细地址','现住地址'=>'现住地址','是否独生子女'=>'是否独生子女','户籍类别'=>'户籍类别','籍贯'=>'籍贯','生源地'=>'生源地','爸爸姓名'=>'爸爸姓名','爸爸电话'=>'爸爸电话','爸爸是否监护人'=>'爸爸是否监护人','爸爸户口所在地'=>'爸爸户口所在地','妈妈现住址'=>'妈妈现住址','妈妈姓名'=>'妈妈姓名','妈妈电话'=>'妈妈电话','妈妈是否监护人'=>'妈妈是否监护人','妈妈户口所在地'=>'妈妈户口所在地','妈妈现住址'=>'妈妈现住址');
		Excel::toExcel($result["rows"],'学生信息',$header);
		exit;
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("年级号", $all)) {
				$array = explode("_", $all["年级号"]);
				$all["年级号"] = $array[0];
				$all["学籍类型"] = $array[1];
			}
			if(array_key_exists("ID", $all)) {
				update('学生信息', $all);
			} else {
				insert('学生信息', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::table("学生信息")->delete(explode(",", $delIds));
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	function importExcel() {
	    //上传excel文件
	    $file = request()->file('files');
	    if(!$file) {
	    	return "请选择文件";
	    }
	    $data = Excel::importExcel($file);
	    if(count($data) == 0) {
	    	return "没有数据！";
	    }
	    try {
    		$errorList = [];
    		$resultList = [];
    		foreach ($data as $idx => $obj) {
    			$result = $obj;
    			if (!empty($obj["年级号"])) {
    				$gl = gradeMap();
    				foreach ($gl as $key => $value) {
						if($value == $obj["年级号"]) {
							$array = explode("_", $key);
							$result["年级号"] = $array[0];
    						$result["学籍类型"] = $array[1];
							break;
						}
					}
    			}
    			$resultList[$idx] = $result;
    		}
			Db::table('学生信息')->insertAll($resultList);
	    } catch (\Exception $e) {
	    	return $e->getMessage();
	    }
	    return "000";
	}

}
